package com.example.woke1;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.CheckpaypswActivity;
import com.example.woke.PswchangeActivity;
import com.example.woke.ZcaipayokActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;

public class PaytofriendActivity extends Activity {

	private EditText mEmoney,mEinfo;
	private ImageView mIView;
	private TextView mTname,mTphone,mTok;
	private MyApp application;
	private String userid,avatar,user_login,user_nicename;
	private String money;
	private String note;
	private RelativeLayout mBar;
	private ImageLoader imageLoader;
	private AlertDialog alertDialog;
	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_paytofriend);
		
		application = (MyApp) getApplication();
		intent = getIntent();
		userid = intent.getStringExtra("userid");
		avatar = intent.getStringExtra("avatar");
		user_login = intent.getStringExtra("user_login");
		user_nicename = intent.getStringExtra("user_nicename");
		imageLoader = new ImageLoader(this);
		intview();
		
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		super.onDestroy();
	}

	private void intview() {
		findViewById(R.id.avpayfriend_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mBar = (RelativeLayout) findViewById(R.id.avpaysure_progress_bar);
		mEmoney = (EditText) findViewById(R.id.avpaytofriend_edit_money);//���
//		mEmoney.addTextChangedListener(watcher);
		mEinfo = (EditText) findViewById(R.id.avpaytofriend_edit_info);//��ע
		mIView = (ImageView) findViewById(R.id.avpaytofriend_image_inco);//ͷ��
		mTname = (TextView) findViewById(R.id.avpaytofriend_text_name);
		mTphone = (TextView) findViewById(R.id.avpaytofriend_text_phone);
		mTphone.setText(user_login);
		mTname.setText(user_nicename);
		imageLoader.DisplayImage("http://"+avatar, mIView,false, true, true,0);
		mTok = (TextView) findViewById(R.id.avpaytofriend_text_ok);
		mTok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				money = ""+mEmoney.getText().toString().trim();
				note = ""+mEinfo.getText().toString().trim();
				String tmoney = mEmoney.getText().toString().trim();
				boolean textline = false;//�ж�С���λ���Ƿ񳬹�
				int indexOf = tmoney.indexOf('.');
				int length = tmoney.length();
				if(indexOf > -1){
					if((length-indexOf - 1) > 2){
//						mEmoney.setText(tmoney.substring(0, tmoney.length() - 1));
						textline = true;
					}
				}
				if(application.getDatas_load ()==null)return;
				if(null == application.getDatas_load().getPay_pwd() || application.getDatas_load().getPay_pwd().equals("")){
					Toast.makeText(PaytofriendActivity.this, "请设置支付密码", Toast.LENGTH_SHORT).show();
					aralogid1("还未设置支付密码","前往设置？",1);
				}else{
					money = ""+mEmoney.getText().toString().trim();
					if(money.equals("")){
						Toast.makeText(PaytofriendActivity.this, "请输入金额", Toast.LENGTH_SHORT).show();
					}else if(textline == true){
							Toast.makeText(PaytofriendActivity.this, "金额小数点后最多2位", Toast.LENGTH_SHORT).show();
					}else{
						Intent intent1 = new Intent(PaytofriendActivity.this,CheckpaypswActivity.class);
						intent1.putExtra("paymoney", money);
						startActivityForResult(intent1, 700);
					}
				}
			}
		});
	}
	
	
	private void aralogid1(String info,String kll,final int ppp){
		   alertDialog = new AlertDialog.Builder(PaytofriendActivity.this). 
	                setTitle(info). 
	                setMessage(kll). 
//	                setIcon(R.drawable.ic_launcher). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
//	                    Intent intent = new Intent();
	                    		intent.setClass(PaytofriendActivity.this, PswchangeActivity.class);
	                    		intent.putExtra("name", 0);
	                    		startActivity(intent);
	                    		alertDialog.dismiss();
	                    } 
	                }). 
	                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	alertDialog.dismiss();
	                        // TODO Auto-generated method stub  
	                    } 
	                }).create(); 
	        alertDialog.show(); 
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 700 && resultCode == 600){
			paymoney();
		}
	}

	private void paymoney() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(PaytofriendActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "order");
		params.put("a", "transfer");
		params.put("userid", application.getDatas_load().getId());
		params.put("touserid", userid);
		params.put("money", money);
		params.put("note", note);
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("ת转账", "成功"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
//						JSONObject object = response.getJSONObject("data");
//						JSONObject jsonObject = object.getJSONObject("data");
						Toast.makeText(PaytofriendActivity.this, info, Toast.LENGTH_SHORT).show();
						intent.setClass(PaytofriendActivity.this, ZcaipayokActivity.class);
						intent.putExtra("user_nicename", "user_nicename");
						startActivity(intent);
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}
	
}
